package com.yanqu.road.dao.impl.activity.shushan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shushan.ShuShanDiscipleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/3/18 16:17
 */
public class ShuShanDiscipleDataDaoImpl extends TempDataObjectDao<ShuShanDiscipleData> {

    public static String getShuShanDiscipleDataSql = "select * from `t_u_activity_shushan_disciple_data` where `activityId`=? ";
    public Map<Long, List<ShuShanDiscipleData>> getShuShanDiscipleDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<ShuShanDiscipleData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuShanDiscipleDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuShanDiscipleData item = parse(rs);
                List<ShuShanDiscipleData> list = map.get(item.getUserId());
                if(list == null){
                    list = new ArrayList<>();
                    map.put(item.getUserId(), list);
                }
                list.add(item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateShuShanDiscipleDataSql = "update `t_u_activity_shushan_disciple_data` set name=?,quality=?,lastLearnedTime=?,getTime=?,isDelete=?,propertiesInfoMap=?,learnedSkillMap=? where activityId=? and userId=? and id=?";
    @Override
    protected boolean updateData(ShuShanDiscipleData shuShanDiscipleData) {
        shuShanDiscipleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, shuShanDiscipleData.getName()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getLastLearnedTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getGetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuShanDiscipleData.getIsDelete()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanDiscipleData.getPropertiesInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanDiscipleData.getLearnedSkillMap())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanDiscipleData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getId()));
        if (getDb().executeNoneQuery(updateShuShanDiscipleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuShanDiscipleDataSql = "insert into `t_u_activity_shushan_disciple_data`(activityId,userId,id,name,quality,lastLearnedTime,getTime,isDelete,propertiesInfoMap,learnedSkillMap) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShuShanDiscipleData shuShanDiscipleData) {
        shuShanDiscipleData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanDiscipleData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getId()));
        para.put(i++, new DbParameter(Types.VARCHAR, shuShanDiscipleData.getName()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getLastLearnedTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanDiscipleData.getGetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuShanDiscipleData.getIsDelete()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanDiscipleData.getPropertiesInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanDiscipleData.getLearnedSkillMap())));
        if (getDb().executeNoneQuery(insertShuShanDiscipleDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuShanDiscipleData parseData(ResultSet rs) throws SQLException {
        ShuShanDiscipleData shuShanDiscipleData = new ShuShanDiscipleData();
        shuShanDiscipleData.setActivityId(rs.getInt("activityId"));
        shuShanDiscipleData.setUserId(rs.getLong("userId"));
        shuShanDiscipleData.setId(rs.getInt("id"));
        shuShanDiscipleData.setName(rs.getString("name"));
        shuShanDiscipleData.setQuality(rs.getInt("quality"));
        shuShanDiscipleData.setLastLearnedTime(rs.getInt("lastLearnedTime"));
        shuShanDiscipleData.setGetTime(rs.getInt("getTime"));
        shuShanDiscipleData.setIsDelete(rs.getBoolean("isDelete"));
        shuShanDiscipleData.setPropertiesInfoMap(JSON.parseObject(rs.getString("propertiesInfoMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shushan.ShuShanPropertiesInfo>>() {}));
        shuShanDiscipleData.setLearnedSkillMap(JSON.parseObject(rs.getString("learnedSkillMap"), new TypeReference<java.util.Map<java.lang.Integer, java.util.Map<java.lang.Integer, java.lang.Integer>>>() {}));

        return shuShanDiscipleData;
    }

}
